
-- --------------------------------------------------
-- Entity Designer DDL Script for SQL Server 2005, 2008, and Azure
-- --------------------------------------------------
-- Date Created: 05/19/2012 12:47:45
-- Generated from EDMX file: C:\Users\Roi\Documents\Visual Studio 2010\Projects\AspB\DAL\DAL\HouseKeepingModel.edmx
-- --------------------------------------------------

SET QUOTED_IDENTIFIER OFF;
GO
USE [Housekeeping_MALAM];
GO
IF SCHEMA_ID(N'dbo') IS NULL EXECUTE(N'CREATE SCHEMA [dbo]');
GO

-- --------------------------------------------------
-- Dropping existing FOREIGN KEY constraints
-- --------------------------------------------------

IF OBJECT_ID(N'[dbo].[FK_ActionTypeExpenses]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Expenses] DROP CONSTRAINT [FK_ActionTypeExpenses];
GO
IF OBJECT_ID(N'[dbo].[FK_UsersExpenses]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Expenses] DROP CONSTRAINT [FK_UsersExpenses];
GO
IF OBJECT_ID(N'[dbo].[FK_UsersIncome]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Incomes] DROP CONSTRAINT [FK_UsersIncome];
GO
IF OBJECT_ID(N'[dbo].[FK_UsersOutcome]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Outcomes] DROP CONSTRAINT [FK_UsersOutcome];
GO
IF OBJECT_ID(N'[dbo].[FK_UserTypesActionType]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[ActionTypes] DROP CONSTRAINT [FK_UserTypesActionType];
GO
IF OBJECT_ID(N'[dbo].[FK_UserTypesUsers]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Users] DROP CONSTRAINT [FK_UserTypesUsers];
GO

-- --------------------------------------------------
-- Dropping existing tables
-- --------------------------------------------------

IF OBJECT_ID(N'[dbo].[ActionTypes]', 'U') IS NOT NULL
    DROP TABLE [dbo].[ActionTypes];
GO
IF OBJECT_ID(N'[dbo].[Expenses]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Expenses];
GO
IF OBJECT_ID(N'[dbo].[Incomes]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Incomes];
GO
IF OBJECT_ID(N'[dbo].[Outcomes]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Outcomes];
GO
IF OBJECT_ID(N'[dbo].[Users]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Users];
GO
IF OBJECT_ID(N'[dbo].[UserTypes]', 'U') IS NOT NULL
    DROP TABLE [dbo].[UserTypes];
GO

-- --------------------------------------------------
-- Creating all tables
-- --------------------------------------------------

-- Creating table 'Users'
CREATE TABLE [dbo].[Users] (
    [Id] int IDENTITY(1,1) NOT NULL,
    [Username] nvarchar(max)  NOT NULL,
    [Password] nvarchar(max)  NOT NULL,
    [UserTypesId] int  NOT NULL
);
GO

-- Creating table 'UserTypes'
CREATE TABLE [dbo].[UserTypes] (
    [Id] int IDENTITY(1,1) NOT NULL,
    [Type] nvarchar(max)  NOT NULL
);
GO

-- Creating table 'Expenses'
CREATE TABLE [dbo].[Expenses] (
    [Id] int IDENTITY(1,1) NOT NULL,
    [UsersId] int  NOT NULL,
    [Date] datetime  NOT NULL,
    [ActionTypeId] int  NOT NULL,
    [Amount] decimal(18,0)  NOT NULL
);
GO

-- Creating table 'ActionTypes'
CREATE TABLE [dbo].[ActionTypes] (
    [Id] int IDENTITY(1,1) NOT NULL,
    [Type] nvarchar(max)  NOT NULL,
    [UserTypesId] int  NOT NULL
);
GO

-- Creating table 'Incomes'
CREATE TABLE [dbo].[Incomes] (
    [Id] int IDENTITY(1,1) NOT NULL,
    [UsersId] int  NOT NULL,
    [Date] datetime  NOT NULL,
    [Amount] decimal(18,0)  NOT NULL
);
GO

-- Creating table 'Outcomes'
CREATE TABLE [dbo].[Outcomes] (
    [Id] int IDENTITY(1,1) NOT NULL,
    [UsersId] int  NOT NULL,
    [Date] datetime  NOT NULL,
    [Amount] decimal(18,0)  NOT NULL
);
GO

-- --------------------------------------------------
-- Creating all PRIMARY KEY constraints
-- --------------------------------------------------

-- Creating primary key on [Id] in table 'Users'
ALTER TABLE [dbo].[Users]
ADD CONSTRAINT [PK_Users]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'UserTypes'
ALTER TABLE [dbo].[UserTypes]
ADD CONSTRAINT [PK_UserTypes]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'Expenses'
ALTER TABLE [dbo].[Expenses]
ADD CONSTRAINT [PK_Expenses]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'ActionTypes'
ALTER TABLE [dbo].[ActionTypes]
ADD CONSTRAINT [PK_ActionTypes]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'Incomes'
ALTER TABLE [dbo].[Incomes]
ADD CONSTRAINT [PK_Incomes]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'Outcomes'
ALTER TABLE [dbo].[Outcomes]
ADD CONSTRAINT [PK_Outcomes]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- --------------------------------------------------
-- Creating all FOREIGN KEY constraints
-- --------------------------------------------------

-- Creating foreign key on [UserTypesId] in table 'Users'
ALTER TABLE [dbo].[Users]
ADD CONSTRAINT [FK_UserTypesUsers]
    FOREIGN KEY ([UserTypesId])
    REFERENCES [dbo].[UserTypes]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_UserTypesUsers'
CREATE INDEX [IX_FK_UserTypesUsers]
ON [dbo].[Users]
    ([UserTypesId]);
GO

-- Creating foreign key on [UsersId] in table 'Expenses'
ALTER TABLE [dbo].[Expenses]
ADD CONSTRAINT [FK_UsersExpenses]
    FOREIGN KEY ([UsersId])
    REFERENCES [dbo].[Users]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_UsersExpenses'
CREATE INDEX [IX_FK_UsersExpenses]
ON [dbo].[Expenses]
    ([UsersId]);
GO

-- Creating foreign key on [ActionTypeId] in table 'Expenses'
ALTER TABLE [dbo].[Expenses]
ADD CONSTRAINT [FK_ActionTypeExpenses]
    FOREIGN KEY ([ActionTypeId])
    REFERENCES [dbo].[ActionTypes]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_ActionTypeExpenses'
CREATE INDEX [IX_FK_ActionTypeExpenses]
ON [dbo].[Expenses]
    ([ActionTypeId]);
GO

-- Creating foreign key on [UserTypesId] in table 'ActionTypes'
ALTER TABLE [dbo].[ActionTypes]
ADD CONSTRAINT [FK_UserTypesActionType]
    FOREIGN KEY ([UserTypesId])
    REFERENCES [dbo].[UserTypes]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_UserTypesActionType'
CREATE INDEX [IX_FK_UserTypesActionType]
ON [dbo].[ActionTypes]
    ([UserTypesId]);
GO

-- Creating foreign key on [UsersId] in table 'Incomes'
ALTER TABLE [dbo].[Incomes]
ADD CONSTRAINT [FK_UsersIncome]
    FOREIGN KEY ([UsersId])
    REFERENCES [dbo].[Users]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_UsersIncome'
CREATE INDEX [IX_FK_UsersIncome]
ON [dbo].[Incomes]
    ([UsersId]);
GO

-- Creating foreign key on [UsersId] in table 'Outcomes'
ALTER TABLE [dbo].[Outcomes]
ADD CONSTRAINT [FK_UsersOutcome]
    FOREIGN KEY ([UsersId])
    REFERENCES [dbo].[Users]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_UsersOutcome'
CREATE INDEX [IX_FK_UsersOutcome]
ON [dbo].[Outcomes]
    ([UsersId]);
GO

-- --------------------------------------------------
-- Script has ended
-- --------------------------------------------------